PACKAGE_NAME = irony
PACKAGE_VERSION = 1.2.0

SRC_DIR = $(HOME)/.emacs.d/elpa/$(PACKAGE_NAME)-$(PACKAGE_VERSION)/server
BUILD_DIR = $(TEMP)/build-$(PACKAGE_NAME)-$(PACKAGE_VERSION)
INSTALL_DIR = $(HOME)/.emacs.d/irony

LLVM_VERSION = 3.9.1
BINUTILS_VERSION = 2.29.1
GCC_VERSION = 6.4.0
CMAKE_VERSION = 3.6.2
PATCHELF_VERSION = 0.9

BINUTILS_PACKAGE_ROOT=$(DD_TOOLS_ROOT)/$(DD_OS)/package/binutils/$(BINUTILS_VERSION)
GCC_PACKAGE_ROOT=$(DD_TOOLS_ROOT)/$(DD_OS)/package/gcc/$(GCC_VERSION)
LLVM_PACKAGE_ROOT=$(DD_TOOLS_ROOT)/$(DD_OS)/package/llvm/$(LLVM_VERSION)

CMAKE_PACKAGE_ROOT=$(DD_TOOLS_ROOT)/$(DD_OS)/package/cmake/$(CMAKE_VERSION)
PATCHELF_PACKAGE_ROOT=$(DD_TOOLS_ROOT)/$(DD_OS)/package/patchelf/$(PATCHELF_VERSION)

ARGS += -DCMAKE_INSTALL_PREFIX=$(INSTALL_DIR)
ARGS += -DCMAKE_CXX_COMPILER=$(GCC_PACKAGE_ROOT)/bin/g++
ARGS += -DCMAKE_PREFIX_PATH=$(LLVM_PACKAGE_ROOT)

BUILD_ENV += PATH=$(BINUTILS_PACKAGE_ROOT)/bin:$(PATH) 
BUILD_ENV += LD_LIBRARY_PATH=$(GCC_PACKAGE_ROOT)/lib64

.PHONY: build
build:
	mkdir -p $(BUILD_DIR)
	cd $(BUILD_DIR) && env $(BUILD_ENV) $(CMAKE_PACKAGE_ROOT)/bin/cmake $(ARGS) $(SRC_DIR) 
	env $(BUILD_ENV) cmake --build $(BUILD_DIR) --use-stderr --config Release --target install

	$(PATCHELF_PACKAGE_ROOT)/bin/patchelf --set-rpath $(LLVM_PACKAGE_ROOT)/lib:$(GCC_PACKAGE_ROOT)/lib64 \
		$(INSTALL_DIR)/bin/irony-server

.PHONY: clean
clean:
	rm -rf -- $(BUILD_DIR)
